/**
 * Created by Celebration-Grey on 2017/4/19.
 */


weibo.home.init = function () {
    weibo.home.autoLogin();
    weibo.emotion.emotionClick();
    weibo.picture.picUpClick();
    weibo.home.topNav();
};

weibo.home.topNav = function () {
    weibo.home.hoverShow($('.gn_set_list .glyphicon-cog'),$('.gn_set_list .gn_topmenulist_set'));
};

weibo.home.test = function (id) {
    $.ajax({
        type: 'GET',
        url: host + 'weibo_getBowen',
        data: {
            bowen_id: id
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            if (data.status == 200) {
                var $test = data;
                var $bowen = weibo.bowen.bowenLi($test.data['bowen'], $test.data['author'], $test.data['repost']);
                $("#WB_bowen").prepend($bowen);
                // $('#WB_bowen').slideDown('slow');
                // weibo.public.replyClick();
            }
            else if (data.status == 304) {

            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.home.autoLogin = function () {
    $.ajax({
        type: 'POST',
        url: host + 'weibo_auto_login',
        data: {},
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            var $data = data;
            if ($data.status == 200) {
                localStorage.setItem('user_info', JSON.stringify($data.data[0]));
                userInfo = $data.data[0];
                weibo.home.setUserInfo(userInfo);
                weibo.home.pubBowen();
                for (var i = 10; i < 35; i++) {
                    weibo.home.test(i);
                }
            }
            else {
                localStorage.clear();
                setTimeout(function () {
                    window.location.href = 'login.html';
                }, 1000)
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.home.setUserInfo = function ($data) {
    // var $data = $.parseJSON(localStorage.getItem('user_info'));
    var $headline_nickname = $('a.gn_name');
    var $card = $('#skin_cover_s');
    var $head = $('#main-head');
    var $head_img = $head.find('img');
    var $info = $('#main-info');
    var $nickname = $info.find('.name');
    var $following = $info.find('.following');
    var $followed = $info.find('.followed');
    var $bowen = $info.find('.bowen');
    var $level = $info.find('.W_icon_level');
    $card.css('background-image', 'url(' + $data['user_card_img'] + ')');

    $head_img.attr('src', $data['user_head_img']);
    $head_img.attr('alt', $data['user_nickname']);

    $headline_nickname.find('em.S_txt1').html($data['user_nickname']);
    $nickname.html($data['user_nickname']);
    $nickname.attr('title', $data['user_nickname']);

    $following.find('strong').html($data['user_following_count']);
    $followed.find('strong').html($data['user_followed_count']);
    $bowen.find('strong').html($data['user_bowen_count']);
    $level.find('.txt_in span').html('Lv.' + $data['user_level']).attr('title', '微博等级' + $data['user_level']);
    localStorage.setItem('user_id', $data['user_id']);
    if($data['user_bg_img'] != null){
        $('body').css('background-image','url('+$data['user_bg_img']+')');
    }
};


weibo.home.pubBowen = function () {
    if (localStorage.getItem('publisher_' + localStorage.getItem('user_id')) == null) {
        localStorage.setItem('publisher_' + localStorage.getItem('user_id'), "");
    }
    var $bowen_content = $('#bowen_content');
    var $pub_btn = $('#pub_btn');
    $bowen_content.val(decodeURI(localStorage.getItem('publisher_' + localStorage.getItem('user_id'))));
    if ($bowen_content.val() != "") {
        $pub_btn.removeClass('W_btn_a_disable');
    }
    $bowen_content.keyup(function () {
        if ($(this).val() != "") {
            $pub_btn.removeClass('W_btn_a_disable');
        }
        else {
            $pub_btn.addClass('W_btn_a_disable');
        }
    });
    $bowen_content.keydown(function () {
        var content = encodeURI($(this).val());
        localStorage.setItem('publisher_' + localStorage.getItem('user_id'), content);
    });

    $pub_btn.click(function () {
        $(this).addClass('W_btn_a_disable');
        weibo.home.pubBowenRequest($bowen_content.val());
    });
};

weibo.home.pubBowenRequest = function (bowen_content) {
    $.ajax({
        type: 'POST',
        url: host + 'weibo_pubBowen',
        data: {
            bowen_content: bowen_content
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            if (data.status == 200) {
                localStorage.setItem('publisher_' + localStorage.getItem('user_id'), '');
                $('#bowen_content').val('');
                $('.send_succpic').show();
                var $bowen = weibo.bowen.bowenLi(data.data['bowen'], data.data['author'], data.data['repost']);
                $("#WB_bowen").prepend($bowen);
                setTimeout(function () {
                    $('.send_succpic').hide();
                    $('#pub_btn').removeClass('W_btn_a_disable');
                }, 2500)
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    })
};

weibo.home.logOut = function () {
    weibo.home.logOutRequest();
};

weibo.home.logOutRequest = function () {
    $.ajax({
        type: 'GET',
        url: host + 'weibo_logOut',
        data: {},
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            if (data.status == 200) {
                window.location.href = 'login.html';
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    })
};

weibo.home.hoverShow = function($hoverobj, $showobj) {
	var timer;
	$hoverobj.hover(function() {
		$showobj.fadeIn();
	});
	$hoverobj.mouseleave(function() {
		timer = setTimeout(function() {
			$showobj.fadeOut();
		}, 500);
		$showobj.mouseenter(function() {
			clearTimeout(timer);
		});
		$hoverobj.mouseenter(function() {
			clearTimeout(timer);
		});
	});
	$showobj.mouseleave(function() {
		timer = setTimeout(function() {
			$showobj.fadeOut();
		}, 500);
		$showobj.mouseenter(function() {
			clearTimeout(timer);
		});
		$hoverobj.mouseenter(function() {
			clearTimeout(timer);
		});
	});
};